package com.whfp.coky.ui.task.act

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import cn.bingoogolapple.qrcode.core.QRCodeView
import com.hjq.permissions.Permission
import com.whfp.coky.aop.Permissions
import com.whfp.coky.databinding.ActivityScanCodeBinding
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.task.model.TaskChildBean
import com.whfp.coky.ui.task.vm.ScanCodeVM
import com.whfp.coky.widget.clicksDelay


/**
 *
 *
 * created by coky on 2022/3/1
 */
class ScanCodeAct : BaseActivity<ScanCodeVM,ActivityScanCodeBinding>(), QRCodeView.Delegate {


    companion object {
        @Permissions(
            Permission.READ_EXTERNAL_STORAGE,
            Permission.WRITE_EXTERNAL_STORAGE,
            Permission.CAMERA)
        fun start(context: Context,title:String,bean:TaskChildBean?) {
            val intent = Intent(context, ScanCodeAct::class.java)
            intent.putExtras(Bundle().apply {
                this.putString(Constants.TITLE,title)
                this.putSerializable(Constants.BEAN,bean)
            })
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }


    override fun initView() {
//        try {
//            getBundle()!!.getString(Constants.TITLE).let {
//                if (it.isNullOrEmpty()){
//                    vModel.scanType.value = "扫码-臻稀物联"
//                }else{
//                    vModel.scanType.value = it
//                }
//            }
//        }catch (e:NullPointerException){
//            toast("未发现操作意图")
//        }finally {
//            postDelayed({finish()},200)
//        }

        vBinding.ivSgd.clicksDelay {
            vModel.isOpen.value = !vModel.isOpen.value!!
        }

        vModel.scanType.value = getBundle()?.getString(Constants.TITLE,"扫码")

        getBundle()?.getSerializable(Constants.BEAN)?.let {
            vModel.taskBean.value = it as TaskChildBean
        }
    }

    override fun initData() {

    }

    override fun onStart() {
        super.onStart()
        // 打开后置摄像头开始预览，但是并未开始识别
        vBinding.zxingview.startCamera();
        //设置回调
        vBinding.zxingview.setDelegate(this)
    }

//    override fun onResume() {
//        super.onResume()
//        // 打开后置摄像头开始预览，但是并未开始识别
//        vBinding.zxingview.startCamera()
//    }

    override fun onStop() {
        // 关闭摄像头预览，并且隐藏扫描框
        vBinding.zxingview.stopCamera()
        super.onStop()
    }


    override fun onDestroy() {
        vBinding.zxingview.onDestroy() // 销毁二维码扫描控件
        super.onDestroy()
    }


    private fun initQRScan() {
        // 打开后置摄像头开始预览，但是并未开始识别
        vBinding.zxingview.startCamera()
        //设置回调
        vBinding.zxingview.setDelegate(this)

    }

    override fun onScanQRCodeSuccess(result: String) {
        vModel.scanSuccess(result)
    }

    override fun onCameraAmbientBrightnessChanged(isDark: Boolean) {
       vModel.scanBrightnessChanged(isDark)
    }

    override fun onScanQRCodeOpenCameraError() {
        toast("打开相机出错")
    }

}


